package com.newland.starpos.controller.advice;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

import com.newland.starpos.base.exception.CustomException;
import com.newland.starpos.http.ResponseModel;


@ControllerAdvice
public class ControllerExceptionHandler {

	static final String CODE = "code";
	static final String MESSAGE = "message";
	static final int SERVER_ERROR_STATUS = 500;

	private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);

	/**
	 * @Description: 捕获CustomException异常处理
	 * @param request
	 * @param response
	 * @param e
	 * @return Map<String, Object>
	 */
	@ExceptionHandler({ CustomException.class })
	public @ResponseBody ResponseModel processAppBizException(NativeWebRequest request,
			HttpServletResponse response, CustomException e) {

		logger.error(e.getMessage(), e);
		return new ResponseModel(e.getCode(), e.getMessage());
	}

	/**
	 * @Description: 捕获其他所有异常处理
	 * @param request
	 * @param response
	 * @param e
	 * @return Map<String, Object>
	 */
	@ExceptionHandler({ Exception.class })
	public @ResponseBody ResponseModel processOtherException(NativeWebRequest request,
			HttpServletResponse response, Exception e) {

		logger.error(e.getMessage(), e);
		return new ResponseModel(SERVER_ERROR_STATUS, "服务器异常,请稍候再试");
	}
}
